package com.zy.algorithm.v3.array

import com.zy.view.DIYSortView
import com.zy.utils.data.Point
import com.zy.utils.data.auto.AutoIndexElement
import com.zy.utils.data.auto.AutoNodeStep
import com.zy.utils.data.auto.Coordinate
import java.util.ArrayList

//https://leetcode.cn/problems/two-sum/
open class LeetCode0001 : BaseV3ArrayFragment() {
    private var array1 = intArrayOf(-11, 7, 3, 2, 1, 7, -10, 11, 21, 3)
    private var array2 = intArrayOf(11)

    private val baseIX = 2

    private val ass1 = builder.setOriginX(baseIX).setOriginY(3).setV("^").setIndex(999)
        .build(Point.Type.SUB_SCRIPT)
    private val ass2 = builder.setOriginX(baseIX + 1).setOriginY(4).setV("^").setIndex(888)
        .build(Point.Type.SUB_SCRIPT)

    private val indexI = builder.setOriginX(1).setOriginY(3).setV("i=0").setIndex(555)
        .setCircle(true)
        .build(Point.Type.DEFAULT)
    private val indexJ = builder.setOriginX(1).setOriginY(4).setV("j=1").setIndex(444)
        .setCircle(true)
        .build(Point.Type.DEFAULT)

    private val nodeI =
        builder.setOriginX(baseIX).setOriginY(5).setV("-11").setIndex(777).build(Point.Type.DEFAULT)

    private val nodeJ = builder.setOriginX(baseIX + 1).setOriginY(5).setV("7").setIndex(666)
        .build(Point.Type.DEFAULT)


    private val sumNode = builder.setOriginX(13).setOriginY(5).setV("-4").setIndex(333)
        .build(Point.Type.DEFAULT)

    override fun getInitD(): Int {
        return 150
    }

    override fun setData(diyV4: DIYSortView) {
        title.text = "方法一:双for循环"
        //diyV4.changeType = 1
        diyV4.setD(getInitD())

        diyV4.create(2, 2, array1, Point.Type.DEFAULT)
        diyV4.create(13, 2, array2, Point.Type.DEFAULT)

        //diyV4.addNoMoveAssistList(1, 5, array2, Point.Type.DEFAULT)

        //diyV4.addAss(1, 1, "V")
        addAss()
    }

    private fun indexAddAdd() {
        if (array1[i] + array1[j] == 11) {
            return
        }

        j++
        if (j == array1.size) {
            i++
            j = i + 1
        }

        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()

        //下标
        indexI.v = "i=$i"
        indexJ.v = "j=$j"

        //数值
        nodeI.v = array1[i].toString()
        nodeJ.v = array1[j].toString()

        //求和
        sumNode.v = (array1[i] + array1[j]).toString()

        val stepList = listOf(
            AutoIndexElement(999, Coordinate(baseIX + i, 3)),
            AutoIndexElement(888, Coordinate(baseIX + j, 4)),
            AutoIndexElement(777, Coordinate(baseIX + i, 5)),
            AutoIndexElement(666, Coordinate(baseIX + j, 5))
        )
        list.add(AutoNodeStep(stepList))

        diyV4.autoMove(list)
    }


    private var i = 0
    private var j = i + 1

    override fun autoClick() {
        indexAddAdd()
    }

    private fun addAss() {
        diyV4.addPoint(ass1)
        diyV4.addPoint(ass2)

        diyV4.addPoint(nodeI)
        diyV4.addPoint(nodeJ)

        diyV4.addPoint(indexI)
        diyV4.addPoint(indexJ)

        diyV4.addPoint(sumNode)
        diyV4.addPoint(
            builder.setOriginX(12).setOriginY(5).setV("=").setIndex(222)
                .build(Point.Type.SUB_SCRIPT)
        )
    }
}